define(function () {
    return Vue.extend({
        data: function () {
            return {
                urlMap: {
                    fetch: '',
                    fetchMethod: 'get',
                    destroy: '',
                    destroyMethod: 'delete',
                    insert: '',
                    insertMethod: 'post',
                    update: '/Shake.Schedule/ScheduleTask/update',
                    updateMethod: 'post'
                },
                identity: 'Id'
            }
        },
        methods: {
            init: function (options, callback) {
                $.extend(this.urlMap, options || {});
            },
            fetch: function (model, options) {
                var url = (options && options.url) ? options.url : this.urlMap.fetch;
                var method = (options && options.method) ? options.url : this.urlMap.fetchMethod;

                this.interation(url, method, model, options);
            },
            destroy: function (model, options) {
                var url = (options && options.url) ? options.url : this.urlMap.destroy;
                var method = (options && options.method) ? options.url : this.urlMap.destroyMethod;

                var args = [];
                if (Object.prototype.toString.call(model) === '[object Array]') {
                    $.each(model, function (index, item) {
                        args.push(itemp[this.identity]);
                    });
                } else {
                    args.push(itemp[this.identity]);
                }
                this.interation(url, method, args, options);
            },
            save: function (model, options) {
                var url = null;
                var method = null;

                if (model[this.identity]) {
                    url = (options && options.url) ? options.url : this.urlMap.update;
                    method = (options && options.method) ? options.url : this.urlMap.updateMethod;
                } else {
                    url = (options && options.url) ? options.url : this.urlMap.insert;
                    method = (options && options.method) ? options.url : this.urlMap.insertMethod;
                }
                this.interation(url, method, model, options);
            },
            /*交互*/
            interation: function (url, method, data, options) {
                options = options || {};
                $.ajax({
                    url: url,
                    method: method,
                    data: JSON.stringify(data || {}),
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        if (options.success) options.success(response);
                    },
                    fail: function () {
                        if (options.fail) options.fail();
                    }
                });
            }
        }
    });
});